package com.ag.hmk.action;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import com.ag.bean.TbCasekw;
import com.ag.bean.TbClassiccase;
import com.ag.bean.TbUri;
import com.ag.common.AnnexPath;
import com.ag.common.BaseAction;
import com.ag.common.IDUtil;
import com.ag.hmk.service.impl.HmkUiltServImpl;
import com.ag.hmk.service.impl.WordToHtmlSerImpl;
import com.ag.lzm.dao.TbUriDAO;
/**
 * 
 * @author hmk
 * 查询一条案例信息
 */
public class FindOneCaseAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private HmkUiltServImpl hmkUiltServImpl;
	private WordToHtmlSerImpl wordToHtmlSerImpl;
	@Resource(name = "LZMTbUriDAO")
	private TbUriDAO uriDAO;
	
	private String caseID1; //案例ID
	private String findType1; //查询类别（导向到不同的页面）
	
	private TbClassiccase caseRst;
	private String caseKeyword;
	private String htmlName;   //附件的HTML文档名
	/*
	 * 查询一个法律
	 */
	public String findOneCase()throws Exception{
		TbClassiccase tbcase = null;
		String keyword = "";
		try {
			String hql = "from TbClassiccase c left join fetch c.tbType left join fetch c.tbLevel left join fetch c.tbCasekws where 1=1";
			System.out.println("------"+caseID1);
			if (caseID1!=null && caseID1.length()>0) {
				hql += " and c.a='"+IDUtil.PRESERVE+"' and c.ccId = " + caseID1;
				List result=null;
				result = hmkUiltServImpl.hqlQuery(hql);
				if (result!=null) { //拼接案例关键字
					tbcase = (TbClassiccase)result.iterator().next();
					Set<TbCasekw> ckwSet = null ;
					ckwSet=( Set<TbCasekw>)tbcase.getTbCasekws();
					Iterator<TbCasekw> it2 =ckwSet.iterator();
					while(it2.hasNext()){
						keyword +=(it2.next().getCKw()+",");
					}
				}
				
			} else {
				System.out.println("-----caseID1 is null");
			}
		} catch (Exception e) {
			e.printStackTrace();
			tbcase = null;
			keyword = "";
		}
		
		this.setCaseRst(tbcase); //设置返回结果
		this.setCaseKeyword(keyword); //设置返回结果
		if (findType1!=null && findType1.equals("1")) {
			System.out.println("-----type:"+findType1);
			//返回到案例编辑
			return "goCaseEidtOK";
		}else{ 
			//返回到案例详情
			if (tbcase!=null) {
				try {
					//处理文件的转换
					String myname =  tbcase.getCcFjName();
					String name = myname.substring(0, myname.lastIndexOf("."));
					System.out.println("nanm"+name);
					String objPath1 = getServletContext().getRealPath("/")+"hmkuploads/";
					String objPath =objPath1+name+".html";
					String srcPath  ="";
					TbUri uri1= uriDAO.findById(AnnexPath.CASE_ANNEX_PATH);
					if (uri1!=null) {
						srcPath = uri1.getUPath()+tbcase.getCcFjName();
					} else {
						srcPath = objPath1+tbcase.getCcFjName();
					}
					//判断文件是否存在
					if (wordToHtmlSerImpl.isFileExist(objPath)) {
						this.htmlName = name+".html"; 
					} else {
						//进行文件转换并进行文件复制
						if(wordToHtmlSerImpl.wordToHtml(srcPath, objPath)){
							this.htmlName = name+".html"; 
							System.out.println("===ok");
						}else{
							this.htmlName="";
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
					this.htmlName="";
				}
			} //if end
			return "findOneCaseOK";
		}
		
		
	}

	public void setHmkUiltServImpl(HmkUiltServImpl hmkUiltServImpl) {
		this.hmkUiltServImpl = hmkUiltServImpl;
	}

	public TbClassiccase getCaseRst() {
		return caseRst;
	}

	public void setCaseRst(TbClassiccase caseRst) {
		this.caseRst = caseRst;
	}

	public String getCaseKeyword() {
		return caseKeyword;
	}

	public void setCaseKeyword(String caseKeyword) {
		this.caseKeyword = caseKeyword;
	}

	public void setCaseID1(String caseID1) {
		this.caseID1 = caseID1;
	}

	public void setFindType1(String findType1) {
		this.findType1 = findType1;
	}

	public String getHtmlName() {
		return htmlName;
	}

	public void setHtmlName(String htmlName) {
		this.htmlName = htmlName;
	}

	public void setWordToHtmlSerImpl(WordToHtmlSerImpl wordToHtmlSerImpl) {
		this.wordToHtmlSerImpl = wordToHtmlSerImpl;
	}

}
